<?php
namespace app\shop\controller\admin;

use app\admin\library\AdminController;

class Page extends AdminController
{
    public function onIndex(){
        $this->power('shop.page');
        $this->pageTitle = '页面管理';
        $this->pageIcon = 'fa-file-text-o';
        return $this->display();
    }

    public function onList(){
        $pageCurrent = $this->post('page',$this->check()->type('int')->default(1));
        $keyword = $this->post('keyword',$this->check()->type('string'));

        $page = $this->model('Page');
        
        if ($keyword){
            $page->where(['title','name'] ,'like' ,'%'.$keyword.'%');
        }

        

        $list = $page->page($pageCurrent,20)->getData();

        foreach($list as &$v){
            $v['create_time'] = date('Y-m-d',$v['create_time']);
        }


        return ['list'=>$list,'total'=>$page->total,'count'=>$page->pageCount];
    }

    public function onSave(){
        $name  = $this->post('name',$this->check()->type('string')->reg('/^[a-zA-Z][a-zA-Z0-9_]{2,50}$/', '页面标识只能由字母、数字、下划线组成，字母开头,长度在3-50个字符之间'));

        $data['title'] = $this->post('title',$this->check()->type('string')->required());
        
        $data['description'] = $this->post('description',$this->check()->type('string'));
        $id = $this->post('page_id',$this->check()->type('int'));

        $page = $this->model('Page');

        
        if ($name){
            $page->where('name',$name);
            if ($id) $page->where('page_id','<>',$id);
            if ($page->count()>0){
                throw new \Exception('页面标识已存在');
            }
            $page->clear();
        }

        if ($id){


            $oldData = $page->getId($id);

            if ($oldData['status'] == 'yes' ){
                throw new \Exception('页面正在使用中，不能修改');
            }

            $data['name'] = $name;
            
            if ($page->updateId($id,$data)){
                $this->log('修改页面',['page_id'=>$id]);
                return true;
            }else{
                throw new \Exception('保存失败');
            }
        }else{

            $data['create_time'] = time();
            $data['status']= 'no';
            $data['name'] = $name;
            if ($pageId = $page->insert($data)){

                if ($name=='') $page->updateId($pageId,['name'=>'page'.$pageId]);
                $this->log('添加页面',['page_id'=>$page->getInsertId()]);
                return true;
            }else{
                throw new \Exception('保存失败');
            }
        }


    }

    public function onDel(){
        $page_id =  $this->post('page_id',$this->check()->type('int')->required());

        $page =  $this->model('Page');

        $data = $page->getId($page_id);

        if (!$data) throw new \Exception('页面不存在');
        if ($data['status'] == 'yes' ){
            throw new \Exception('页面正在使用中，不能删除');
        }else{
            if ($page->deleteId($page_id)){

                unset($data['content']);
                unset($data['style']);

                $this->log('删除页面',['page_id'=>$data]);
                return true;
            }else{
                throw new \Exception('删除失败');
            }
        }
    }

    public function onCold(){
        $page_id =  $this->post('id',$this->check()->type('int')->required());
        $page = $this->model('Page');
        $cold = $this->post('cold',$this->check()->type('int')->default(0));
        if ($page->updateId($page_id,['cold'=>$cold])){
            
            return true;
        }

        throw new \Exception('修改失败');
    }

    public function onGetDesignId(){
        $page_id =  $this->post('page_id',$this->check()->type('int')->required());

        $pageDesign = $this->model('PageDesign');

        $data = $pageDesign->where('page_id',$page_id)->order('update_time','desc')->getRow();

        if ($data){
            return ['design_id'=>$data['design_id']];
        }else{
            //插入一个设计稿
            $design_id = $pageDesign->insert([
                'page_id'=>$page_id,
                'title'=>'草稿',
                'update_time'=>time(),
            ]);

            return ['design_id'=>$design_id];
        }
    }

    public function onDesign(){
        $design_id =  $this->get('id',$this->check()->type('int')->required());


        $pageDesign = $this->model('PageDesign');

        $designData = $pageDesign->getId($design_id);
        if (!$designData){
            throw new \Exception('设计稿不存在');
        }

        
        $page_id = $designData['page_id'];



        $page = $this->model('Page');
        $data = $page->getId($page_id);

        $this->power('shop.page');
        $this->pageTitle = $data['title'] .'- 页面设计';
        $this->pageIcon = 'fa-magic';
        $this->set('data',$data);
        $this->set('title',$data['title']);
        $this->set('page_id',$page_id);

        $this->set('design_id',$design_id);
        $this->set('designList',$pageDesign->field(['design_id','title','update_time'])->where('page_id',$page_id)->order('update_time','desc')->getData());

        return $this->displayBlank();
    }

    public function onSaveDesign(){
        $design_id =  $this->post('design_id',$this->check()->type('int')->required());
        $content =  $this->post('content',$this->check()->type('string')->required());
        $style =  $this->post('style',$this->check()->type('string')->required());
        $update_time =  $this->post('update_time',$this->check()->type('int')->required());


        $PageDesign = $this->model('PageDesign');
        
        $data = $PageDesign->getId($design_id);

        if (!$data){
            throw new \Exception('设计稿不存在');
        }

        if ($update_time != $data['update_time']){
            throw new \Exception('数据已过期，请刷新页面后重试');
        }
        
        
        $new_update_time  =  time();
        
        if ($PageDesign->updateId($design_id,['content'=>$content,'style'=>$style,'update_time'=>$new_update_time])){
            $this->log('设计稿保存',['design_id'=>$design_id]);
            return ['update_time'=>$new_update_time];
        }else{
            throw new \Exception('保存失败');
        }
    }

    public function onSaveDesignOther(){
        $page_id =  $this->post('page_id',$this->check()->type('int')->required());
        $title =  $this->post('title',$this->check()->type('string')->required());
        $content =  $this->post('content',$this->check()->type('string')->required());
        $style =  $this->post('style',$this->check()->type('string')->required());

        $PageDesign = $this->model('PageDesign');

        $data = [
            'page_id'=>$page_id,
            'title' => $title,
            'content' => $content,
            'style' => $style,
            'update_time' => time(),
        ];
         
        $design_id =  $PageDesign->insert($data);

        if ($design_id){
            $list  = $PageDesign->field(['design_id','title','update_time'])->where('page_id',$page_id)->order('update_time','desc')->getData();
            $data['design_id'] = $design_id;
            unset($data['content']);
            unset($data['style']);
            $this->log('设计稿另存为',$data);

            return ['list'=>$list,'design_id'=>$design_id];
        }else{
            throw new \Exception('保存失败');
        }



    }

    public function onGetDesign(){
        $design_id =  $this->post('design_id',$this->check()->type('int')->required());
        $PageDesign = $this->model('PageDesign');
        $data = $PageDesign->getId($design_id);

        $re['content'] = json_decode($data['content'],true);
        $re['style'] = json_decode($data['style'],true);
        $re['update_time'] = $data['update_time'];

        return $re;
    }

    public function onRenameDesign(){
        $design_id =  $this->post('design_id',$this->check()->type('int')->required());
        $title =  $this->post('title',$this->check()->type('string')->required());

        $PageDesign = $this->model('PageDesign');
        $data = $PageDesign->getId($design_id);

        if (!$data) throw new \Exception('设计稿不存在');

        if ($PageDesign->updateId($design_id,['title'=>$title])){

            unset($data['content']);
            unset($data['style']);
            $data['newTitle'] = $title;
            $this->log('重命名设计稿',$data);
            return true;
        }
    }

    public function onDelDesign(){
        $design_id =  $this->post('design_id',$this->check()->type('int')->required());
        
        $PageDesign = $this->model('PageDesign');
        $data = $PageDesign->getId($design_id);

        if (!$data) throw new \Exception('设计稿不存在');

        if ($PageDesign->where('page_id',$data['page_id'])->count()==1){
            throw new \Exception('不能删除最后一个设计稿');
        }

        if ($PageDesign->deleteId($design_id)){
            unset($data['content']);
            unset($data['style']);
            $this->log('删除设计稿',$data);
            return true;
        }else{
            throw new \Exception('删除失败');
        }
    }

    public function onPublish(){
        $design_id =  $this->post('design_id',$this->check()->type('int')->required());
        $content =  $this->post('content',$this->check()->type('string')->required());
        $style =  $this->post('style',$this->check()->type('string')->required());
        $PageDesign = $this->model('PageDesign');
        $data = $PageDesign->getId($design_id);

        if (!$data) throw new \Exception('设计稿不存在');

        $page = $this->model('Page');

        $pageData['content'] =  $content;
        $pageData['style'] =  $style;

        
        if ($page->updateId($data['page_id'],$pageData)){

            $update_time =  time();
            $pageData['update_time'] =  $update_time;
            $PageDesign->updateId($design_id,$pageData);

            $logData = $page->field('page_id,title')->getId($data['page_id']);
            $logData['design_id'] = $design_id;

            $this->log('发布页面',$logData);
            return ['update_time'=>$update_time];
        }else{
            throw new \Exception('发布失败');
        }
    }
}